package Classification

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"os"
)

type DeleteClassification struct {
	beego.Controller
}

func (c *DeleteClassification) DeleteClassification() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Classification/DeleteClassification"
	//res := resMes.GetAdminUser{}
	//user := []*Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 1)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	err = DeleteProductM2M(o, id)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(key)

	sql := "delete from `activity_classifications` where `classification_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	sql = "delete from `coupon_classifications` where `classification_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	sql = "delete from `small_class_classifications` where `classification_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	sql = "delete from `classification_product_detaileds` where `classification_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	classification := &Admin.Classification{ClassificationId: id}
	err = o.Read(classification)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	if classification.PrimariesImage != "" {
		_ = os.Remove("." + classification.PrimariesImage)

	}

	num, err := o.QueryTable("Classification").Filter("ClassificationId__exact", id).Delete()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	} else if num == 0 {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "400", "失败")
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}

func DeleteProductM2M(o orm.Ormer, id string) (err error) {
	classification := &Admin.Classification{ClassificationId: id}
	sql := "select * from `product_detailed` left join `classification_product_detaileds` on product_detailed.product_id=classification_product_detaileds.product_detailed_id where classification_product_detaileds.classification_id=?"
	_, err = o.Raw(sql, id).QueryRows(&classification.ProductDetailed)

	if err != nil {
		return
	}
	if len(classification.ProductDetailed) != 0 {
		m2m := o.QueryM2M(classification, "ProductDetailed")
		_, err = m2m.Remove(classification.ProductDetailed)
	}
	return err
}
